<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**	
 * A Default Controller Resolver
 *
 * Resolve a controller using the request parameters "component" and "action". 
 * The resolver composes the controller name using following rules:
 * <code>
 * tx_<extension>_<component>_<action>_Controller. 
 * </code>
 * 
 * If no controller is found with this name a second pattern is used as folowed:
 * <code>
 * tx_<extension>_<component>s_Controller 
 * </code>
 * 
 * Controllers have to be located in PHP's include_path to be considered.
 * 
 * @package auxo
 * @subpackage Web
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_ControllerDefaultResolver implements tx_auxo_ControllerResolverInterface {
	
	/**
	 * The Extension Environment
	 *
	 * @var tx_auxo_ExtensionEnvironment
	 */
	protected $environment = NULL;
	
	/**
	 * Sets the Extension Environment
	 *
	 * @param tx_auxo_ExtensionEnvironment $enviroment
	 */
	public function setExtensionEnvironment(tx_auxo_ExtensionEnvironment $enviroment) {
		$this->environment = $environment;	
	}
	
	/**
	 * Resolve a controller using the request parameters "component" and "action". 
	 * 
	 * First of all, component and action are used to find a Controller 
	 * named tx_<extension>_<component>_<action_Controller. If not found, a controller 
	 * named tx_<extension>_<component>s_Controller will be searched. Controller classes
	 * have to be located in PHP's include_path to be considered.
	 *
	 * @param tx_auxo_Request $request a web request
	 * @return string $className returns the classname of controller or an empty string
	 */
	public function resolve(tx_auxo_Request $request) {
		if (($component = $this->request->getParameters()->has('component'))) {
			if (($action = $this->request->getParameters()->has('action'))) {
				$className = 'tx_' . $this->environment->getExtension() . '_' . ucfirst($component) . '_' . ucfirst($action) . '_Controller';
				if (class_exists($className))	{
					return $className;
				}
			}
			$className = 'tx_' . $this->environment->getExtension() . '_' . ucfirst($component) . 's' . '_Controller';
			if (class_exists($className)) {
				return $className;
			}
		}
		
		return '';
	}
}
?>